package com.iot.demo.messge;

import com.alibaba.fastjson.JSON;
import com.ux.iot.jetlinks.bo.JetlinkSend;
import com.ux.iot.jetlinks.message.function.FunctionInvokeMessage;
import com.ux.iot.jetlinks.message.function.FunctionParameter;
import com.ux.iot.jetlinks.service.websocket.SingleEndPointAsyncIotMsgService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.UUID;
import java.util.concurrent.Future;

/**
 * @author caixiadao
 * @version 1.0
 * 发送消息实例
 */
@Component
@Slf4j
public class MessageHandlerManager {

    //发送消息处理类
    @Autowired
    SingleEndPointAsyncIotMsgService singleEndPointAsyncIotMsgService;


    /**
     * 下发消息到 sendComm功能，命令格式：01 03 04 03 9A 00 EF 9B D4
     * @param projectId 产品ID
     * @throws Exception
     */
    public void sendCommandHandler(String projectId) throws Exception {
        FunctionInvokeMessage functionInvokeMessage=new FunctionInvokeMessage();
        functionInvokeMessage.setFunctionId("sendComm");// 功能ID
        functionInvokeMessage.addInput(FunctionParameter.builder().name("cmd").value("01 03 04 03 9A 00 EF 9B D4").build());
        JetlinkSend<FunctionInvokeMessage> js= JetlinkSend.<FunctionInvokeMessage>builder()
                .type("sub").parameter(functionInvokeMessage)
                .id(UUID.randomUUID().toString())
                .topic("/device-message-sender/"+projectId+"/*").build();

        Future<Void> future= singleEndPointAsyncIotMsgService.send(JSON.toJSONString(js));

    }
}
